package com.anzhen.interview.concurrent;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by zhenan on 2017-4-7.
 */
public class LockTest {
    private static Lock lock = new ReentrantLock();
    static AtomicInteger atomicInteger = new AtomicInteger(0);


    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    add();
                }
            }).start();
        }
    }

    public static void add() {
        lock.lock();
        try {
            atomicInteger.incrementAndGet();
            System.out.println(Thread.currentThread().getName() + "---" + atomicInteger.get());
        }finally {
            lock.unlock();
        }
    }

}
